package tech.lq0.dreamaticvoyage.item.misc;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.type.capability.ICurioItem;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class GourmetMedal extends Item implements ICurioItem {

    private static final List<MobEffect> EFFECTS = new ArrayList<>();

    static {
        EFFECTS.add(MobEffects.MOVEMENT_SLOWDOWN);
        EFFECTS.add(MobEffects.DIG_SLOWDOWN);
        EFFECTS.add(MobEffects.CONFUSION);
        EFFECTS.add(MobEffects.HUNGER);
        EFFECTS.add(MobEffects.POISON);
        EFFECTS.add(MobEffects.UNLUCK);
        EFFECTS.add(MobEffects.WITHER);
        EFFECTS.add(MobEffects.WEAKNESS);
        EFFECTS.add(MobEffects.BLINDNESS);
    }

    public GourmetMedal() {
        super(new Properties().stacksTo(1).fireResistant().rarity(Rarity.EPIC));
    }

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
        pTooltipComponents.add(Component.translatable("des.dreamaticvoyage.gourmet_medal_1").withStyle(ChatFormatting.GRAY));
        pTooltipComponents.add(Component.translatable("des.dreamaticvoyage.gourmet_medal_2").withStyle(ChatFormatting.GRAY).withStyle(ChatFormatting.ITALIC));
    }

    @Override
    public boolean canEquip(SlotContext slotContext, ItemStack stack) {
        LivingEntity livingEntity = slotContext.entity();
        AtomicBoolean flag = new AtomicBoolean(true);
        CuriosApi.getCuriosInventory(livingEntity).ifPresent(c -> c.findFirstCurio(this).ifPresent(s -> flag.set(false)));

        return flag.get();
    }

    @Override
    public void curioTick(SlotContext slotContext, ItemStack stack) {
        LivingEntity entity = slotContext.entity();

        for (MobEffect mobEffect : EFFECTS) {
            if (entity != null && entity.hasEffect(mobEffect)) {
                MobEffectInstance effectInstance = entity.getEffect(mobEffect);
                if (effectInstance != null) {
                    entity.removeEffect(mobEffect);
                }
            }
        }

        ICurioItem.super.curioTick(slotContext, stack);
    }
}
